import { request } from "@/utils/service"
import * as Files from "@/api/files/types/files"
import { getToken } from "@/utils/cache/cookies"

export function uploadFileApi(data: FormData) {
  return request<Files.UploadFileResponseData>({
    url: "file/upload",
    method: "post",
    headers: {
      "Content-Type": "multipart/form-data",
      Authorization: "Bearer " + getToken()
    },
    data
  })
}

/**
 * 审核
 * @param data
 */
export function auditFileApi(params: Files.IAuditRequestData) {
  return request<Files.AuditFileResponseData>({
    url: "file_admin/audit",
    method: "put",
    params
  })
}

/**
 * 重传
 * @param id
 * @param file
 */
export function reloadFileApi(id: number, file: File) {
  return request<Files.AuditFileResponseData>({
    url: "file/reload",
    method: "put",
    params: { id },
    data: { file }
  })
}

/**
 * 获取审批记录
 * @param params
 * @param data
 */
export function getFileApplyListApi(params: Files.IGetApplyRequestQueryData, data: Files.IGetApplyListRequestData) {
  return request<Files.GetApplyListResponseData>({
    url: "file/getApplyList",
    method: "post",
    params,
    data
  })
}

export function fileDownloadApi(uid: number, ids: number[]) {
  return request<Files.FileDownloadResponseData>({
    url: `file/download`,
    method: "post",
    responseType: "blob",
    params: { userId: uid },
    data: { ids }
  })
}

export function getFileMultiConditionQueryApi(
  params: { page: number; size: number },
  data: Files.IGetMultiConditionQueryRequestData
) {
  return request<Files.GetMultiConditionQueryResponseData>({
    url: "file/multiConditionQuery",
    method: "post",
    params,
    data
  })
}

export function fileApplyApi(data: Files.IUploadRequestData) {
  return request<Files.ApplyFileResponseData>({
    url: "file/apply",
    method: "post",
    data
  })
}

export function deleteFilesApi(data: { ids: number[]; applyId: number }) {
  return request({
    url: `file_admin/deleteFiles?applyId=${data.applyId}`,
    method: "delete",
    data: { ids: data.ids }
  })
}
